package m.jclecai.com.activity;

import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;
import m.jclecai.com.R;

public class ShareSDKDemo extends AppCompatActivity {

    @BindView(R.id.btn_back)
    Button mBtnBack;

    @BindView(R.id.btn_tan)
    Button mBtnTan;

    @BindView(R.id.ll_bottom)
    LinearLayout mLlBottom;

    @BindView(R.id.view_blank)
    View mViewBlank;

    @BindView(R.id.activity_share_sdkdemo)
    RelativeLayout mActivityShareSdkdemo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_share_sdkdemo);

        ButterKnife.bind(this);


        mBtnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //这里我们要做的一个操作就是分享微信
                //shareWeChat();
                //   showShare();
            }
        });


        mLlBottom.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                mLlBottom.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                mLlBottom.setTranslationY(mLlBottom.getHeight());
            }
        });

        mBtnTan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //打开；
                OpenRulesAnimation();
            }
        });

        mViewBlank.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                closeRulesAnimation();
            }
        });

    }

    private void OpenRulesAnimation() {
        ObjectAnimator.ofFloat(mLlBottom, "TranslationY", mLlBottom.getHeight(), 0).setDuration(300).start();
    }

    private void closeRulesAnimation() {
        ObjectAnimator.ofFloat(mLlBottom, "TranslationY", 0, mLlBottom.getHeight()).setDuration(300).start();
    }


    public void shareWeChat(String platformName) {

        Platform.ShareParams wechatMoments = new Platform.ShareParams();
        wechatMoments.setTitle("乐彩神彩票");
        wechatMoments.setText("快我们吧！！！");
        wechatMoments.setUrl("http://m.yuecp.net/");
        wechatMoments.setImageUrl("http://f1.sharesdk.cn/imgs/2014/02/26/owWpLZo_638x960.jpg");
        // wechatMoments.setMusicUrl("http://www.zhlongyin.com/UploadFiles/xrxz/2011/5/201105051307513619.mp3");
        wechatMoments.setShareType(Platform.SHARE_WEBPAGE);
        Platform weixin = ShareSDK.getPlatform(platformName);
        weixin.setPlatformActionListener(new PlatformActionListener() {
            @Override
            public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                //
                Toast.makeText(ShareSDKDemo.this, "成功", Toast.LENGTH_SHORT).show();
                System.out.println("成功");
            }

            @Override
            public void onError(Platform platform, int i, Throwable throwable) {

                Toast.makeText(ShareSDKDemo.this, "失败", Toast.LENGTH_SHORT).show();
                System.out.println("失败");
            }

            @Override
            public void onCancel(Platform platform, int i) {

                Toast.makeText(ShareSDKDemo.this, "取消", Toast.LENGTH_SHORT).show();
                System.out.println("取消");
            }
        });
        weixin.share(wechatMoments);

    }


    private void showShare() {
        OnekeyShare oks = new OnekeyShare();
        //关闭sso授权
        oks.disableSSOWhenAuthorize();

        // title标题，印象笔记、邮箱、信息、微信、人人网和QQ空间等使用
        oks.setTitle("标题");
        // titleUrl是标题的网络链接，QQ和QQ空间等使用
        oks.setTitleUrl("http://sharesdk.cn");
        // text是分享文本，所有平台都需要这个字段
        oks.setText("我是分享文本");
        // imagePath是图片的本地路径，Linked-In以外的平台都支持此参数
        //oks.setImagePath("/sdcard/test.jpg");//确保SDcard下面存在此张图片
        // url仅在微信（包括好友和朋友圈）中使用
        oks.setUrl("http://sharesdk.cn");
        // comment是我对这条分享的评论，仅在人人网和QQ空间使用
        oks.setComment("我是测试评论文本");
        // site是分享此内容的网站名称，仅在QQ空间使用
        oks.setSite(getString(R.string.app_name));
        // siteUrl是分享此内容的网站地址，仅在QQ空间使用
        oks.setSiteUrl("http://sharesdk.cn");

        // 启动分享GUI
        oks.show(this);
    }
}
